﻿using Microsoft.EntityFrameworkCore;
using Super.Admin.EFCore;
using Super.Admin.Entities;
using Super.Admin.IServices;

namespace Super.Admin.Services
{
    /// <summary>
    /// 公告管理服务实现类
    /// </summary>
    public class AnnouncementService : BaseService, IAnnouncementService
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">操作的数据库上下文</param>
        public AnnouncementService(SuperDataBaseDbContext context) : base(context) { }
        /// <summary>
        /// 查询所有公告
        /// </summary>
        /// <returns>公告管理实体集合</returns>
        public IReadOnlyCollection<AnnouncementEntity> GetAnnouncements()
        {
            return Query<AnnouncementEntity>(a => a.Status == 0)
                  .Include(a => a.CreateUser)
                  .Include(a => a.UpdateUser)
                  .Include(a => a.PublishUser)
                  .OrderByDescending(a => a.PublishTime)
                  .ToList();
        }
        /// <summary>
        /// 查询公告
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        /// <param name="beginTime">创建开始时间</param>
        /// <param name="endTime">创建结束时间</param>
        /// <param name="createUserId">创建人</param>
        /// <param name="updateUserId">修改人</param>
        /// <param name="status">状态：0：草稿，1：发布</param>
        /// <param name="pageSize">一页数量</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="totalCount">总数</param>
        /// <returns>公告管理实体集合</returns>
        public IReadOnlyCollection<AnnouncementEntity> GetAnnouncements(string title, string content, string beginTime, string endTime, int createUserId, int updateUserId, int status, int pageSize, int pageIndex, out int totalCount)
        {
            var dataResult = QueryPage<AnnouncementEntity, string>(
                a => (string.IsNullOrEmpty(title) || a.Title.Contains(title)) &&
                   (string.IsNullOrEmpty(content) || a.Content.Contains(content)) &&
                   (string.IsNullOrEmpty(beginTime) || a.CreateTime.CompareTo(beginTime) > 0) &&
                   (string.IsNullOrEmpty(endTime) || a.CreateTime.CompareTo(endTime) < 0) &&
                   (createUserId == -1 || a.CreateUserId == createUserId) &&
                   (updateUserId == -1 || a.UpdateUserId == updateUserId) &&
                   (status == -1 || a.Status == status),
                   pageSize, pageIndex, a => a.CreateTime, false,
                   a => a.Include(a => a.CreateUser),
                   a => a.Include(a => a.UpdateUser),
                   a => a.Include(a => a.PublishUser));
            totalCount = dataResult.TotalCount;
            return dataResult.DataList;
        }
        /// <summary>
        /// 创建公告
        /// </summary>
        /// <param name="announcementEntity">公告类</param>
        /// <param name="userId">创建人</param>
        public void CreateAnnouncement(AnnouncementEntity announcementEntity, int userId)
        {
            if (announcementEntity == null)
            {
                throw new ArgumentNullException(nameof(announcementEntity));
            }
            if (userId == 0 || userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            announcementEntity.Status = 1; // 默认设置为草稿状态
            announcementEntity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            announcementEntity.CreateUserId = userId;
            Insert<AnnouncementEntity>(announcementEntity);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="announcementEntity">公告实体</param>
        /// <param name="userId">修改人</param>
        public void UpdateAnnouncement(AnnouncementEntity announcementEntity, int userId)
        {
            if (announcementEntity == null)
            {
                throw new ArgumentNullException(nameof(announcementEntity));
            }
            if (userId == 0)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            announcementEntity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            announcementEntity.UpdateUserId = userId;
            Update<AnnouncementEntity>(announcementEntity);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 发布公告/取消发布公告
        /// </summary>
        /// <param name="announcementId">需要发布/取消的公告Id</param>
        /// <param name="userId">操作人Id</param>
        public void PublishAnnouncement(int announcementId, int userId)
        {
            if (announcementId == 0)
            {
                throw new ArgumentNullException(nameof(announcementId));
            }
            if (userId == 0)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            var announcement = Query<AnnouncementEntity>(a => a.Id == announcementId).FirstOrDefault();
            if (announcement == null)
            {
                throw new Exception("公告不存在");
            }
            if (announcement.Status == 1)
            {
                announcement.PublishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                announcement.PublishUserId = userId;
            }
            else
            {
                announcement.PublishTime = null;
                announcement.PublishUserId = null;
            }
            announcement.Status = announcement.Status == 0 ? 1 : 0; // 设置为发布状态
            Update<AnnouncementEntity>(announcement);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 批量删除公告
        /// </summary>
        /// <param name="announcementIds">批量删除的Id</param>
        public void DeleteAnnouncements(IEnumerable<int> ids)
        {
            if (ids.Count() == 0 || !ids.Any())
            {
                throw new ArgumentNullException(nameof(ids));
            }
            Deletes<AnnouncementEntity, int>(ids);
            base.Commit(); // 提交事务
        }
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="announcementId">公告ID</param>
        public void DeleteAnnouncement(int announcementId)
        {
            if (announcementId == 0)
            {
                throw new ArgumentNullException(nameof(announcementId));
            }
            Delete<AnnouncementEntity>(announcementId);
            base.Commit(); // 提交事务
        }
    }
}
